package com.panshi.controller;

import com.panshi.service.IReplyAuditService;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.ReplyAudit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Api(tags = "回答审核业务接口")
@Slf4j
@Controller
@RequestMapping(value = "/replyAudit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ReplyAuditController {

    @Autowired
    IReplyAuditService replyAuditService;

    //添加回答
    @PostMapping(value = "/add")
    @ResponseBody
    @ApiOperation("回答审核添加")
    @ApiImplicitParam(name = "replyAudit",value = "回答审核信息",required = true)
    public DataResultQA add(@RequestBody ReplyAudit replyAudit){
        log.info("请求路径：{},参数：{}","/replyAudit/add",replyAudit);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = replyAudit.getRContent() == null && replyAudit.getRQuestionId() == 0 &&replyAudit.getRUserId() == 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = replyAuditService.addReplyAudit(replyAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("添加完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("添加失败！");
        }
        return dataResultQA;
    }

    //分页查询
    @PostMapping(value = "/inquirePages")
    @ResponseBody
    @ApiOperation("分页查询")
    @ApiImplicitParam(name = "pages",value = "分页查询条件",required = true)
    public DataResultQA<Pages<ReplyAudit>> inquirePages(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/replyAudit/inquirePages",pages);
        DataResultQA<Pages<ReplyAudit>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<ReplyAudit> p = replyAuditService.inquirePagesReplyAudit(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

    //查询总记录数
    @GetMapping(value = "/inquireSum")
    @ResponseBody
    @ApiOperation("查询总记录条数")
    @ApiImplicitParam(name = "pages",value = "查询条件",required = true)
    public DataResultQA<Integer> inquireSum(@RequestParam Pages pages){
        log.info("请求路径：{},参数：{}","/replyAudit/inquireSum",pages);
        DataResultQA<Integer> dataResultQA = new DataResultQA();

        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Integer sum = replyAuditService.inquireReplyAuditSum(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询数据条数！");
        dataResultQA.setData(sum);
        return dataResultQA;
    }

    //通过id查询回答
    @PostMapping(value = "/inquireById/{id}")
    @ResponseBody
    @ApiOperation("通过id查询")
    @ApiImplicitParam(name = "id",value = "回答审核id",required = true)
    public DataResultQA<ReplyAudit> inquireById(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/replyAudit/inquireById/{}",id);
        DataResultQA<ReplyAudit> dataResultQA = new DataResultQA<>();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        ReplyAudit replyAudit =  replyAuditService.inquireById(id);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据id查询！");
        dataResultQA.setData(replyAudit);
        return dataResultQA;
    }

    //修改内容（用户）
    @PostMapping(value = "/update")
    @ResponseBody
    @ApiOperation("修改内容（用户）")
    @ApiImplicitParam(name = "replyAudit",value = "回答审核信息",required = true)
    public DataResultQA update(@RequestBody ReplyAudit replyAudit){
        log.info("请求路径：{},参数：{}","/replyAudit/update",replyAudit);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = replyAudit.getRContent() != null;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        boolean boo = replyAuditService.updateReplyAudit(replyAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("修改完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("修改失败！");
        }
        return dataResultQA;
    }

    //删除(标记删除)
    @GetMapping(value = "/delete/{id}")
    @ResponseBody
    @ApiOperation("删除(标记删除)")
    @ApiImplicitParam(name = "id",value = "回答审核id",required = true)
    public DataResultQA delete(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/replyAudit/delete/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = replyAuditService.deleteReplyAudit(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("标记删除完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("标记删除失败！");
        }
        return dataResultQA;
    }

    //修改审核状态<审核>（管理员）
    @GetMapping(value = "/updateState")
    @ResponseBody
    @ApiOperation("修改审核状态<审核>（管理员）")
    @ApiImplicitParam(name = "replyAudit",value = "回答审核信息",required = true)
    public DataResultQA updateState(@RequestParam ReplyAudit replyAudit){
        log.info("请求路径：{},参数：{}","/replyAudit/updateState",replyAudit);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = replyAudit.getRState() == 0 || replyAudit.getCheckDescribe() ==null;//审核状态不等于0,审核描述不等于null

        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        boolean boo = replyAuditService.updateState(replyAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("审核完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("审核失败！");
        }
        return dataResultQA;
    }
}
